package com.white.algebra.utils;

import com.white.algebra.domain.Syslog;
import com.white.algebra.service.SysLogService;
import com.white.algebra.service.impl.SysLogServiceImpl;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQObjectMessage;

import javax.jms.*;
import java.io.Serializable;
import java.sql.SQLException;

public class MqUtils {
    private static final String ACTIVEMQ_URL="tcp://192.168.111.136:61616";
    private static final String QUEUE_NAME="log";
    private static Session session;
    private static MessageProducer messageProducer;
    private static MessageConsumer consumer;

    public static void innitial() throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setTrustAllPackages(true);
        Connection connection=activeMQConnectionFactory.createConnection();
        connection.start();
        //第一个参数事务，第二个参数签收
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination=session.createQueue(QUEUE_NAME);
        MessageProducer messageProducer=session.createProducer(destination);
        MessageConsumer consumer=session.createConsumer(destination);
        MqUtils.session =session;
        MqUtils.messageProducer =messageProducer;
        MqUtils.consumer =consumer;
    }



    public static void sendQueueMessage(Syslog log) throws JMSException {
        ActiveMQObjectMessage msg = (ActiveMQObjectMessage) MqUtils.session.createObjectMessage();
        msg.setObject((Serializable) log);
        MqUtils.messageProducer.send(msg);
    }


    public static void receiveQueueMessage() throws JMSException, SQLException {
        while (true){
            ActiveMQObjectMessage msg;
            msg= (ActiveMQObjectMessage) MqUtils.consumer.receive();
            if(null!=msg){
                Syslog log= (Syslog) msg.getObject();
                SysLogService sysLogService=new SysLogServiceImpl();
                sysLogService.addSysLog(log);
            }
        }

    }


    public static void main(String[] args) throws JMSException, SQLException {
//        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory();
//        Connection connection=activeMQConnectionFactory.createConnection();
//        connection.start();
//        //第一个参数事务，第二个参数签收
//        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Destination destination=session.createQueue(QUEUE_NAME);
//        MessageProducer messageProducer=session.createProducer(destination);
//        for(int i=0;i<3;i++){
//            TextMessage mes=session.createTextMessage("--mes--"+i);
//            messageProducer.send(mes);
//        }
//        messageProducer.close();
////        MessageConsumer consumer=session.createConsumer(destination);
////        while (true){
////            TextMessage textMessage= (TextMessage) consumer.receive();
////            if(null!=textMessage){
////                System.out.println("mes="+textMessage);
////            }else {
////                break;
////            }
////        }
//        session.close();
//        connection.close();
//        System.out.println("mq 发布完成！");
        MqUtils.innitial();
        for (int i = 0; i <2 ; i++) {
            Syslog log=new Syslog();
            log.setUsername("testMq t"+i);
            MqUtils.sendQueueMessage(log);
        }


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    MqUtils.receiveQueueMessage();
                } catch (JMSException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
